// pages/cliniclist/cliniclist.js
var common = require('../../js/common.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    datalist: [],
    hidden: true,
    nocancel: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    var userInfo = wx.getStorageSync('userInfo');
    //没有openid
    this.setData({
      yjyz: wx.getStorageSync('userInfo').type
    })

    if (!userInfo) {
      wx.redirectTo({
        url: '/pages/index/index'
      })
      return;
    }

    var that = this;


    //获取积分
    if (wx.getStorageSync('tongshi')) {
      common.request(app.globalData.tongshiUrl, 'zhenshoulist', {
          user_id: wx.getStorageSync('userInfo').id
        },
        function(res) {
          console.log('同事诊所列表', res)
          if (res.data.code == 1) {
            that.setData({
              datalist: res.data.data,
              tongshi: 1
            })
          }
        }, 'rtype= post');
    } else {

      common.request(app.globalData.localUrl, 'yiyuanlist', {
          user_id: wx.getStorageSync('userInfo').id
        },
        function(res) {
          console.log('诊所列表', res)
          if (res.data.code == 1) {

            that.setData({
              datalist: res.data.data
            })
          }
        }, 'rtype= post');

    }


  },

  //编辑
  bianji: function(e) {
    console.log("id =" + e.target.dataset.id)
    var id = e.target.dataset.id;
    wx.navigateTo({
      url: '/pages/clinicedit2/clinicedit2?id=' + id,
    })
  },

  //切换用户
  qiehuan: function() {
    wx.clearStorage();
    wx.redirectTo({
      url: '/pages/index/index',
    })
  },
  //详情
  reservation: function(e) {
    var id = e.target.dataset.id;
    console.log("id =" + e.target.dataset.id)
    wx.setStorageSync('yiyuan_id', e.target.dataset.id);
    var that = this;
    //获取积分
    common.request(app.globalData.localUrl, 'authuser', {
        yiyuan_id: e.target.dataset.id,
        user_id: wx.getStorageSync('userInfo').id
      },
      function(res) {
        console.log('权限列表', res)
        // console.log(res.data.data.gsy_status)
        if (wx.getStorageSync('userInfo').type == 1 || res.data[13] || res.data['14']) {
          wx.navigateTo({
            url: '/pages/reservation/reservation',
          })
        }
      }, 'rtype= post');

  },

  //删除

  shanchu: function(e) {
    var id = e.target.dataset.id;
    this.setData({
      hidden: false,
      delid: id
    });
  },
  cancel: function() {
    this.setData({
      hidden: true
    });
  },
  confirm: function() {
    this.setData({
      hidden: true
    });

    var that = this;

    if (wx.getStorageSync('tongshi')) {
      //获取积分
      common.request(app.globalData.tongshiUrl, 'delzhenshou', {
          zhenshou_id: that.data.delid,
          user_id: wx.getStorageSync('userInfo').id
        },
        function(res) {
          console.log('删除诊所', res)
          if (res.data.code == 1) {
            that.onLoad();
          }
        }, 'rtype= post');

    } else {
      //获取积分
      common.request(app.globalData.localUrl, 'delyiyuan', {
          id: that.data.delid
        },
        function(res) {
          console.log('删除诊所', res)
          if (res.data.code == 1) {
            that.onLoad();
          }
        }, 'rtype= post');
    }


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    //获取积分
    that.onLoad();

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})